/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETSI_Rows.cc
  \date			september 2011
  \author		TNick

  \brief		Contains the implementation for ETSI_Rows class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QFontMetrics>
#include	<QPainter>

#include	<sheet/ETSheetView.h>
#include	<sheet/ETSheet.h>
#include	<sheet/subitems/ETSI_Root.h>
#include	<sheet/subitems/ETSI_SText.h>

#include	"ETSI_Rows.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
ETSI_Rows::ETSI_Rows	( void )
	: ETSubItem()
{

	ETSI_Root * row_1 = new ETSI_Root();
	ETSI_SText * row_1s1 = new ETSI_SText();
	row_1->append(row_1s1);
	ETSI_SText * row_1s2 = new ETSI_SText();
	row_1->append(row_1s2);
	ctr_block.append(row_1);

	ETSI_Root * row_2 = new ETSI_Root();
	ETSI_SText * row_2s1 = new ETSI_SText();
	row_2->append(row_2s1);
	ETSI_SText * row_2s2 = new ETSI_SText();
	row_2->append(row_2s2);
	ctr_block.append(row_2);




}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_Rows::~ETSI_Rows	( void )
{

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Rows::recalcVAlign		(void)
{
	QFontMetrics f_m(font());
	setVAlignSize(f_m.height() * 0.1);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Rows::hitTest			(HtData * h_data)
{
	ETSI_Root * itr_row = first();

	while (itr_row != NULL)
		{
			if (itr_row->hitEmbAid(h_data, 0, itr_row->posX())) return;
			itr_row = static_cast<ETSI_Root *>(itr_row->next());
		}

	h_data->it_hitted = this;
	h_data->true_hit = true;
	/* h_data->rct_bbx already set by the caller */
	h_data->int_data = 0;

}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void				ETSI_Rows::paint		(
	ETSheetView * vp, QPainter * painter, QPointF pt_ref )
{

	PaintData	pd(vp, painter, pt_ref);
	prePaint(&pd);

	ETSI_Root * itr_row = first();
	qreal orig_y = pt_ref.y();

	while (itr_row != NULL)
		{
			/* x position is same as input */
			pt_ref.setY(orig_y + itr_row->posX());
			itr_row->paint(vp, painter,pt_ref);

			itr_row = static_cast<ETSI_Root *>(itr_row->next());
		}

	postPaint(&pd);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Rows::recompute	(bool b_full)
{


	if (b_full == false)
		{ /* only recompute if this element is "dirty" */

			/* the SIZE_DIRTY flag means that we should recompute the sigma size
  and all items that rely on this font. */
			if (isSizeDirty())
				{
					ctr_block.recompute(true);
				}
			else if (isKidSizeDirty())
				{
					if (ctr_block.requireRecompute())
						{
							ctr_block.recompute(b_full);
						}
				}
			else
				{
					return;
				}
		}
	else
		{ /* forced recompute */
			ctr_block.recompute(b_full);
		}


	/* get the size of graphics based on the font that is used */
	recalcVAlign();

	/* we need to walk once to compute full size */
	qreal width_cm = 0;
	qreal height_cm = 0;

	ETSI_Root * itr_row = first();
	while (itr_row != NULL)
		{
			width_cm = width_cm < itr_row->width() ?
						itr_row->width() :
						width_cm;

			height_cm += itr_row->height() + vAlignSize();

			itr_row = static_cast<ETSI_Root *>(itr_row->next());
		}
	height_cm -= vAlignSize();
	height_cm = height_cm / 2;

	bbox.setCoords(0, - height_cm, width_cm, height_cm);

	/* now we need to walk the chain oce again to set y positions */
	itr_row = first();
	height_cm = - height_cm;
	while (itr_row != NULL)
		{
			height_cm += itr_row->height();
			itr_row->setPosX(
						height_cm + /* position of bounding box at top */
						itr_row->boundingBox().y() /* adjust to ref point */
						);
			height_cm += vAlignSize();
			itr_row = static_cast<ETSI_Root *>(itr_row->next());
		}


	/* reset the flags that demand a recompute */
	resetSizeFlags();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		ETSI_Rows::loadXMLBasic		(
	QDomElement dom_e)
{
	QDomElement		dom_it;

	/* the one and only common data to all kinds of sub-items */
	dom_it = dom_e.firstChildElement(XML_TAG_COMM);
	if (!dom_it.isNull())
		{
			ETSubItem::loadXMLCmn(this, dom_it);

			if (!dom_it.nextSiblingElement(XML_TAG_COMM).isNull())
				{
					qDebug() << "The input presented more than one "
							 << "common tags, when only one per sub-item element "
							 << "is allowed";
				}
		}

	/* a list of components must be present here */


	dom_it = dom_e.firstChildElement(XML_TAG_ROW);
	while (!dom_it.isNull())
		{
			ETSI_Root * new_row = new ETSI_Root();
			ctr_block.append(new_row);

			new_row->loadEbdXmlRootSi(new_row, dom_it);
			dom_it = dom_it.nextSiblingElement(XML_TAG_ROW);
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_Rows *		ETSI_Rows::loadXML		(
	QDomElement dom_e)
{

	ETSI_Rows *		ret_it;

#ifdef	APP_DEBUG
	/* type is assumed to be sum */

	QDomElement dom_it = dom_e.firstChildElement(XML_TAG_TYPE);
	if (!dom_it.isNull())
		{
			Q_ASSERT_X(dom_it.text() == XML_SIT_ROWS, __FUNCTION__,
					   QString("loadXML got an element of type <"
							   "%1> while expected type is " XML_SIT_ROWS)
					   .arg(dom_it.text()).toLatin1()
					   );
		}
#endif

	/* create this element */
	ret_it = new ETSI_Rows();

	ret_it->loadXMLBasic(dom_e);

	return ret_it;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Rows::saveXMLBasic		(
	QDomElement dom_e)
{
	QDomElement dom_it;
	QDomElement dom_it2;

	/* save common properties */
	dom_it = dom_e.ownerDocument().createElement(XML_TAG_COMM);
	dom_e.appendChild(dom_it);
	saveXMLCmn(dom_it);


	/* now specifics */
	ETSI_Root * itr_row = first();
	while (itr_row != NULL)
		{
			dom_it = dom_e.ownerDocument().createElement(XML_TAG_ROW);
			dom_e.appendChild(dom_it);
			dom_it2 = dom_e.ownerDocument().createElement(XML_TAG_SUBITEM);
			dom_it.appendChild(dom_it2);
			itr_row->saveXML(dom_it2);

			itr_row = static_cast<ETSI_Root *>(itr_row->next());
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Rows::saveXML		(
	QDomElement dom_e)
{

	/* save the type */
	QDomElement dom_it = dom_e.ownerDocument().createElement(XML_TAG_TYPE);
	dom_e.appendChild(dom_it);
	QDomText dom_tx = dom_e.ownerDocument().createTextNode(XML_SIT_ROWS);
	dom_it.appendChild(dom_tx);

	saveXMLBasic(dom_e);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		ETSI_Rows::kidNavReq_up		(ETSheetView * vp, ETSubItem * kid)
{
//	if (kid == &top_block)
//		{
//			if (fatherSubI() != NULL)
//				{
//					fatherSubI()->kidNavReq_up(vp, this);
//					return true;
//				}
//		}
//	else if (kid == &btm_block)
//		{
//			vp->setCurrentSubI(NULL, &top_block);
//			return true;
//		}
	Q_UNUSED(vp);
	Q_UNUSED(kid);

	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		ETSI_Rows::kidNavReq_down		(ETSheetView * vp, ETSubItem * kid)
{
//	if (kid == &top_block)
//		{
//			vp->setCurrentSubI(NULL, &btm_block);
//			return true;
//		}
//	else if (kid == &btm_block)
//		{
//			if (fatherSubI() != NULL)
//				{
//					fatherSubI()->kidNavReq_down(vp, this);
//					return true;
//				}
//		}
	Q_UNUSED(vp);
	Q_UNUSED(kid);

	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QPointF		ETSI_Rows::subIRefPt		(const ETSubItem * p_kid) const
{
	return QPointF(0, p_kid->posX());
}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
